﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="TestWebRequest.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagementTests.TestSupport
{
    using System;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;

    public class TestWebRequest : WebRequest
    {
        private readonly TestWebRequestStream requestStream = new TestWebRequestStream();

        private readonly ITestWebResponseFactory responseFactory;

        private readonly Uri requestUri;

        public TestWebRequest(Uri requestUri, ITestWebResponseFactory factory)
        {
            this.requestUri = requestUri;
            this.responseFactory = factory;
        }

        public override WebHeaderCollection Headers { get; set; }

        public override string Method { get; set; }

        public override string ContentType { get; set; }

        public override long ContentLength { get; set; }

        public override Uri RequestUri
        {
            get
            {
                return this.requestUri;
            }
        }

        public string CertificateThumbprint { get; set; }

        public override Stream GetRequestStream()
        {
            return this.requestStream;
        }

        public string GetBody()
        {
            return this.requestStream.GetString();
        }

        public override WebResponse GetResponse()
        {
            return this.responseFactory.CreateResponse(this);
        }

        public override Task<WebResponse> GetResponseAsync()
        {
            return Task.Factory.StartNew(state => this.responseFactory.CreateResponse(this), null);
        }
    }
}